package com.example.springbootredis.util;

import lombok.Data;

@Data
public class BaseResp<T> {
    private Integer code;
    private String message;
    private T data;
    private final static Integer success=0;
    private final static Integer fail=-1;


    public Boolean isSuccess(){
        return success.equals(code);
    }

    public static BaseResp buildSuccess(){
        BaseResp resp = new BaseResp();
        resp.setCode(success);
        return resp;
    }

    public static <T> BaseResp<T> buildSuccess(String message){
        BaseResp resp = new BaseResp();
        resp.setCode(success);
        resp.setMessage(message);
        return resp;
    }
    public static <T> BaseResp<T> buildDataSuccess(T data){
        BaseResp resp = new BaseResp();
        resp.setCode(success);
        resp.setData(data);
        return resp;
    }

    public static BaseResp buildFail(String message){
        BaseResp resp = new BaseResp();
        resp.setCode(fail);
        resp.setMessage(message);
        return resp;
    }
    public static BaseResp buildFail(Integer code,String message){
        BaseResp resp = new BaseResp();
        resp.setCode(code);
        resp.setMessage(message);
        return resp;
    }
    public static Boolean checkSuccess(BaseResp baseResp){
        if(baseResp != null && baseResp.isSuccess()){
            return Boolean.TRUE;
        }
        return Boolean.FALSE;
    }
    public static Boolean checkFail(BaseResp baseResp){
        return !checkSuccess(baseResp);
    }
}
